require(['./config'], () => {
    require(['template', 'request', 'header', 'toTop', 'footer'], (template, request, header) => {
        class Cate {
            constructor() {
                this.init()
                this.toTop()
                this.fideIn()
            }
            init() {
                // 从URL上取到id
                const id = window.location.hash.slice(4)

                // 根据id取请求当前分类的数据
                var start = 0;
                var sort = 0;
                var n;
                request.getCategory(id, start, sort).then(resp => {
                        console.log(resp)
                        if (resp.code === 200) {
                            const { list } = resp.data.items
                            console.log(list)
                            $('#gList').html(template('goodsList', { list }))
                        }
                    })
                    // 向后分页
                $('.good-list').on('click', '#next', function() {
                        start += 20
                        n++
                        request.getCategory(id, start, sort).then(resp => {
                            console.log(resp)
                            if (resp.code === 200) {
                                const { list } = resp.data.items
                                console.log(list)
                                $('#gList').html(template('goodsList', { list }))
                            }
                        })
                        $('#page-n').html(n)
                    })
                    // 排序
                $('.mid-center').on('click', '.sort-price', function() {
                    sort = 1
                    request.getCategory(id, start, sort).then(resp => {
                        console.log(resp)
                        if (resp.code === 200) {
                            const { list } = resp.data.items
                            console.log(list)
                            $('#gList').html(template('goodsList', { list }))
                        }
                    })
                })
                $('.mid-center').on('click', '.sort-num', function() {
                        sort = 2
                        request.getCategory(id, start, sort).then(resp => {
                            console.log(resp)
                            if (resp.code === 200) {
                                const { list } = resp.data.items
                                console.log(list)
                                $('#gList').html(template('goodsList', { list }))
                            }
                        })
                    })
                    // 向前分页
                $('.good-list').on('click', '#prev', function() {
                    if (start == 0) {
                        start = 0;
                        n = 1
                    } else {
                        start -= 20
                    }
                    n--;
                    request.getCategory(id, start, sort).then(resp => {
                        console.log(resp)
                        if (resp.code === 200) {
                            const { list } = resp.data.items

                            $('#gList').html(template('goodsList', { list }))
                        }
                    })
                    $('#page-n').html(n)
                })

            }
            toTop() {
                $('.totop').toTop();
            }
            fideIn() {
                $(window).on('scroll', function() {
                    clearTimeout(timer)
                    var timer = setTimeout(function() {
                        //滚动位置
                        let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                        console.log('滚动条位置' + parseInt(scrollTop));
                        if (parseInt(scrollTop) >= 400) {
                            $('header').css('position', 'fixed')
                            $('header').css('z-index', '666')

                        } else {
                            $('header').css('position', '')
                        }

                    }, 1000)

                })
            }

        }
        new Cate()
    })
})